package com.secondshop.controllers;

import com.secondshop.models.FirstType;
import com.secondshop.models.SecondType;
import com.secondshop.services.GoodService;
import com.secondshop.services.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品类型接口
 */
@Controller
@RequestMapping("type")
public class TypeController {
	private final TypeService typeService;
	private final GoodService goodService;
	//注入service
	@Autowired
	public TypeController(TypeService typeService, GoodService goodService) {
		this.typeService = typeService;
		this.goodService = goodService;
	}

	/*
	 * @RequestMapping(value = "/secondType/{firstTypeId}", method =
	 * RequestMethod.GET) public ResponseEntity getSecondTypeId(@PathVariable
	 * Integer firstTypeId) { List<SecondType> secondTypes = typeService
	 * .getSecondTypeByFirstTypeId(firstTypeId); if (secondTypes == null) { return
	 * ResponseEntity.ok("isNull"); } return ResponseEntity.ok(secondTypes); }
	 */
	/*
	 * @RequestMapping(value = "/firstType/{firstTypeId}", method =
	 * RequestMethod.GET) public ResponseEntity getFirstType(@PathVariable Integer
	 * firstTypeId) { List<FirstType> firstTypes = typeService
	 * .getFirstTypeById(firstTypeId); if (firstTypes == null) { return
	 * ResponseEntity.ok("isNull"); } return ResponseEntity.ok(firstTypes);
	 * 
	 * }
	 */

	/**
	 * 删除二级分类
	 * @param secondTypeId
	 * @return
	 */
	@RequestMapping(value = "/secondType/delete/{secondTypeId}", method = RequestMethod.GET)
	public ResponseEntity deleteSecondType(@PathVariable Integer secondTypeId) {
		Boolean success = goodService.getGoodsAdminByType(secondTypeId)
				.isEmpty();
		System.out.println(goodService.getGoodsAdminByType(secondTypeId));
		if (success) {
			Integer thisFirstTypeId = typeService.getSecondTypeById(
					secondTypeId).getFirstTypeId();
			success = typeService.deleteSecondType(secondTypeId);
			if (success) {
				List<SecondType> secondTypeList = typeService
						.getSecondTypeByFirstTypeId(thisFirstTypeId);
				if (secondTypeList == null) {
					return ResponseEntity.ok("isNull");
				}
				return ResponseEntity.ok(secondTypeList);
			}
		}
		return ResponseEntity.ok(success);
	}

	/**
	 * 删除一级分类
	 * @param firstTypeId
	 * @return
	 */
	@RequestMapping(value = "/firstType/delete/{firstTypeId}", method = RequestMethod.GET)
	public ResponseEntity deleteFirstType(@PathVariable Integer firstTypeId) {
		Boolean success = typeService.getSecondTypeByFirstTypeId(firstTypeId)
				.isEmpty();
		if (success) {
			success = typeService.deleteFirstType(firstTypeId);
			if (success) {
				List<FirstType> firstTypeList = typeService.getAllFirstType();
				if (firstTypeList == null) {
					return ResponseEntity.ok("isNull");
				}
				return ResponseEntity.ok(firstTypeList);
			}
		}
		return ResponseEntity.ok(success);
	}

	/**
	 * 创建二级分类
	 * @param secondType
	 * @return
	 */
	@RequestMapping(value = "/secondType/create", method = RequestMethod.POST)
	public ResponseEntity createSecondType(@RequestBody SecondType secondType) {
		Integer thisFirstTypeId = secondType.getFirstTypeId();
		Boolean success = typeService.createSecondType(secondType);
		if (success) {
			List<SecondType> secondTypeList = typeService
					.getSecondTypeByFirstTypeId(thisFirstTypeId);
			return ResponseEntity.ok(secondTypeList);
		}
		return ResponseEntity.ok(success);
	}
/*查找二级分类*/
	@RequestMapping(value = "/secondType/{firstTypeId}", method = RequestMethod.GET)
	public ResponseEntity FindSecondTypeByFirstTypeId(@PathVariable Integer firstTypeId)  {

		List<SecondType> secondTypes=  typeService.getSecondTypeByFirstTypeId(firstTypeId);


		return ResponseEntity.ok(secondTypes);
	}


	/**
	 * 创建二级分类
	 * @param firstType
	 * @return
	 */
	@RequestMapping(value = "/firstType/create", method = RequestMethod.POST)
	public ResponseEntity createSecondType(@RequestBody FirstType firstType) {
		Boolean success = typeService.createFirstType(firstType);
		if (success) {
			List<FirstType> firstTypeList = typeService.getAllFirstType();
			return ResponseEntity.ok(firstTypeList);
		}
		return ResponseEntity.ok(success);
	}
}
